<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $ 内容云
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

$return['content_types']=content_types();
$show_types=$_RQ['show_types']?:';text;image;audio;video;file;miniprogram;news;';
$show_types=str_array($show_types);
$tabs=array();
foreach($show_types as $sv){
	$title=content_types()[$sv];
	$tabs[]=array('op'=>'list','query'=>'type='.$sv,'type'=>$sv,'title'=>$title.'活动');
}
if($_RQ['op']=='list'){
	if($_RQ['type']){
		$_SESSION['content_type']=$_RQ['type'];
	}
	model('core/content')->up_article();
	model('core/content')->up_website();
	model('core/content')->up_goods();
	$_SESSION['content_type']=$_SESSION['content_type']?:'text';
	$return['params']=array('type'=>$_SESSION['content_type']);	
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'showtype'=>1,'type'=>$_SESSION['content_type']);
	$group_id=intval($_RQ['group_id']);
	if($group_id){
		$params['group_id']=$group_id;
		$return['params']['group_id']=$group_id;
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='star desc, id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$list=pdo_getslice('core_account_content',$params,array($pindex,$psize),$total,'','id',$orderby);
	foreach($list as &$item){
		$item['content'] = cfc('tools')->substr($item['content'],0,56);
		$item['url']=custom_url('core/article.list/detail/id='.$item['id'],true);
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'group_id','title'=>'分类','type'=>'select','options'=>'groups'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'star','title'=>'星数','type'=>'star','set'=>'star'),
			array('name'=>'image','title'=>'图片','type'=>'image','show'=>array('type <>'=>'text')),
			array('name'=>'content','title'=>'内容','type'=>'string','show'=>array('type'=>'text')),
			array('name'=>'h5url','title'=>'链接','type'=>'string','show'=>array('type <>'=>'miniprogram')),
			array('name'=>'wxappurl','title'=>'小程序路径','type'=>'string','show'=>array('type'=>'miniprogram')),
		)
		),
		'search_one'=>array('name'=>'title','select_name'=>'group_id','select_title'=>'选择分类','options'=>'groups'),
	);
	$return['htmls']=$htmls;
	$return['groups']=pdo_getall('core_account_content_group',array('type'=>$_SESSION['content_type'],'uniacid'=>$_SESSION['uniacid']),'','id');
	//$return['tabs']=$tabs;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_content',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$_SESSION['content_type']);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'编辑'.$return['content_types'][$_SESSION['content_type']],
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'string','title'=>'标题','name'=>'title', 'help'=>''),
			array('name'=>'group_id','title'=>'分类','type'=>'select','options'=>'groups'),
			array('type'=>'text_wx','title'=>'文本内容','name'=>'content','show'=>array('type'=>'text'), 'help'=>''),
			array('type'=>'text','title'=>'描述','name'=>'desc','show'=>array('type'=>'news'),'help'=>''),
			array('type'=>'image','title'=>'图片','name'=>'image','show'=>array('type'=>array('news','image','miniprogram')), 'help'=>''),
			array('type'=>'link','from'=>'action','title'=>'文章链接','name'=>'h5url','show'=>array('type'=>'news'), 'help'=>'' ),
			array('type'=>'audio','title'=>'上传语音','name'=>'file','show'=>array('type'=>'audio'), 'help'=>'' ),
			array('type'=>'video','title'=>'上传视频','name'=>'file','show'=>array('type'=>'video'), 'help'=>'' ),
			array('type'=>'string','title'=>'小程序id','name'=>'wxappid','show'=>array('type'=>'miniprogram'),'help'=>''),
			array('type'=>'string','title'=>'小程序路径','name'=>'wxappurl','show'=>array('type'=>'miniprogram'),'help'=>'类似：/page/index'),
		)
	);
	$return['groups']=pdo_getall('core_account_content_group',array('merchid'=>$_SESSION['onemerchid'],'type'=>$_SESSION['content_type'],'uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'content'=>trim($postdata['content']),
		'desc'=>trim($postdata['desc']),
		'h5url'=>trim($postdata['h5url']),
		'file'=>trim($postdata['file']),
		'wxappid'=>trim($postdata['wxappid']),
		'wxappurl'=>trim($postdata['wxappurl']),
		'group_id'=>intval($postdata['group_id']),
		'image'=>trim($postdata['image'])
	);
	if($postdata['type']=='image' || $postdata['type']=='news' || $postdata['type']=='miniprogram'){
		$data['media_id']=cfc('account')->add_media($postdata['image'],1,$postdata['title']);
	}elseif($postdata['type']=='audio'){
		$data['media_id']=cfc('account')->add_media($postdata['file'],2,$postdata['title']);
	}elseif($postdata['type']=='video'){
		$data['media_id']=cfc('account')->add_media($postdata['file'],3,$postdata['title']);
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_content',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('内容修改成功！','','list');
	}else{
		$data['type']=$_SESSION['content_type'];
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['showtype']=1;
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_account_content',$data);
		exi('内容增加成功！','','list');
	}
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('core_account_content',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	//删除素材
	$del=str_array($_RQ['del']);
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(!$del){
		exi('请先选择要删除的内容！','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('core_account_content',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('删除成功！');
}